package com.iweb.dishes.servlet;

import com.alibaba.fastjson.JSONObject;
import com.iweb.dishes.model.Dishes;
import com.iweb.dishes.service.DishesService;
import com.iweb.dishes.service.DishesServiceImpl;
import com.iweb.util.PageModel;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet(value="*.dish")
public class DishesServlet extends HttpServlet {
    DishesService dishesService=new DishesServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String path=req.getServletPath();
        switch(path){
            case "/toList.dish":
                toList(req,resp);
                break;
            case "/selectByPage.dish":
                selectByPage(req,resp);
                break;
            case "/toEdit.dish":
                toEdit(req,resp);
                break;
            case "/addOrUpdate.dish":
                addOrUpdate(req, resp);
                break;
            case "/deleteById.dish":
                deleteById(req,resp);
                break;
            case "/queryById.dish":
                queryById(req,resp);
                break;
            default:
                break;
        }

    }


    //删除菜品
    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String cid0=req.getParameter("cid");
        int cid= Integer.parseInt(cid0);
        dishesService.deleteById(cid);
        resp.sendRedirect(req.getContextPath()+"/toList.dish?pageNo=1&likeValue=");
    }
//转发去dishes_selectByPage页面
    private void toList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        req.getRequestDispatcher("/dishes/dishes_selectByPage.html").forward(req,resp);
    }


    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String pageNo=req.getParameter("pageNo");
        int pageNoInt=Integer.parseInt(pageNo);//String转为int型
        String likeValue=req.getParameter("likeValue");
        PageModel pageModel=new PageModel();
        pageModel.setPageNo(pageNoInt);
        pageModel.setLikeValue(likeValue);
        List<Dishes> dishes=dishesService.selectByPage(pageModel);
        pageModel.setRecordList(dishes);
        String result= JSONObject.toJSONString(pageModel);
        resp.getWriter().print(result);
    }
    //编辑菜品信息
    private void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        req.getSession().setAttribute("saveCategoryFlag","1");
        String cid=req.getParameter("cid");
        resp.sendRedirect(req.getContextPath()+"/dishes/dishes_toAdd.html?cid="+cid);
    }

    private void addOrUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
            String dish_name=req.getParameter("dish_name");
            String dish_description=req.getParameter("dish_description");
            String price0=req.getParameter("price");
            int price=Integer.parseInt(price0);
            String dish_type=req.getParameter("dish_type");
            String cid=req.getParameter("cid");
            if(cid==null ||"".equals(cid)){
                //新增
                Dishes dishes=new Dishes(null,dish_name,dish_type,dish_description,price);
                dishesService.addOne(dishes);
            }else{
                //修改
                Dishes dishes=new Dishes(Integer.valueOf(cid),dish_name,dish_type,dish_description,price);
                dishesService.updateOne(dishes);
            }
            resp.sendRedirect(req.getContextPath()+"/toList.dish?pageNo=1&likeValue=");

    }


    private void queryById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String cid=req.getParameter("cid");
        Dishes dishes=dishesService.queryById(cid);
        String result=JSONObject.toJSONString(dishes);
        resp.getWriter().println(result);
    }



}
